package com.cvo.scrumtoolkit.client.pannels;

import java.util.ArrayList;

import com.cvo.scrumtoolkit.client.controller.ControllerImpl;
import com.cvo.scrumtoolkit.client.controller.HistoryManager;
import com.cvo.scrumtoolkit.client.controller.Navigator;
import com.cvo.scrumtoolkit.client.controller.View;
import com.cvo.scrumtoolkit.client.entities.LoadedProject;
import com.cvo.scrumtoolkit.client.entities.Project;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.ListBox;

public class MenuPanel extends Composite  {
	
	String view;

	private static MenuPanelUiBinder uiBinder = GWT
			.create(MenuPanelUiBinder.class);

	private final PersistentServiceAsync persistentService = GWT.create(PersistentService.class);
	
	interface MenuPanelUiBinder extends UiBinder<Widget, MenuPanel> {
	}

	public MenuPanel() {
		initWidget(uiBinder.createAndBindUi(this));
		
			persistentService.searchProjects(new AsyncCallback<ArrayList<Project>>() {

				@Override
				public void onFailure(Throwable caught) {
					System.out.println("Projecten konden niet geladen worden in menu");
					
				}

				@Override
				public void onSuccess(ArrayList<Project> result) {
					
					for (final Project project:result){
						listbox.addItem(project.getProjectname());
						
					}
					
				}
			});
	}
	@UiField
	Button home;
	@UiField
	Button productbacklog;
	@UiField
	Button sprintbacklog;
	@UiField
	Button scrumboard;
	@UiField
	Button burndownchart;
	@UiField Label nameLabel;
	@UiField VerticalPanel vp;
	
	
	@UiField ListBox listbox;
	
	
	
	
	@UiHandler("listbox")
void handleList(ChangeEvent e){
		try {
			
			String id =listbox.getValue(listbox.getSelectedIndex());
			String string = "projectbacklog/"+id+"/0";
			HistoryManager.gotoUrl(string);
		
	}catch (Exception ex) {
		// TODO: handle exception
	}			
}
		
	
	
	
	
	@UiHandler("home")
	 void handleHome(ClickEvent e) {
		try{
		HistoryManager.gotoUrl("home/0/0");
			
		}catch (Exception ex) {
			// TODO: handle exception
		}
	}

	
	
	@UiHandler("productbacklog")
	 void handleProductbacklog(ClickEvent e) {
				try {
		
				String id =Navigator.getNavigator().getProjectId();
				String string = "projectbacklog/"+id+"/0";
				HistoryManager.gotoUrl(string);
			
		}catch (Exception ex) {
			// TODO: handle exception
		}			
	}

	@UiHandler("sprintbacklog")
	void handleSprintbacklog(ClickEvent e) {
		try{
			final String id =Navigator.getNavigator().getProjectId();
		
		persistentService.getSprintsInProject(id, new AsyncCallback<Integer>() {
			
			@Override
			public void onSuccess(Integer result) {
				String s=result.toString();
				String string = "sprintbacklog/"+id+"/"+s;
				HistoryManager.gotoUrl(string);				
			}
			
			@Override
			public void onFailure(Throwable caught) {
				String string = "sprintbacklog/"+id+"/0";
				HistoryManager.gotoUrl(string);
				
			}
		});
		
		
				
			
		}catch (Exception ex) {
			// TODO: handle exception
		}
	}

	
	@UiHandler("scrumboard")
	 void handleScrumboard(ClickEvent e) {
		
		
	}
	
	@UiHandler("burndownchart")
	 void handleBurndownChart(ClickEvent e) {
		
	}

	




public void veranderkleur(String view){
	
	if(view.equals("home")){
	System.out.println("in het menu home wordt geschreven:"+view);
	home.setStyleName("blauwknops");
	productbacklog.setStyleName("blauwknop");
	sprintbacklog.setStyleName("blauwknop");
	scrumboard.setStyleName("blauwknop");
	burndownchart.setStyleName("blauwknop");

			
			}
	else if(view.equals("projectbacklog")){
		System.out.println("in het menu productbacl wordt geschreven:"+view);
		home.setStyleName("blauwknop");
		productbacklog.setStyleName("blauwknops");
		sprintbacklog.setStyleName("blauwknop");
		scrumboard.setStyleName("blauwknop");
		burndownchart.setStyleName("blauwknop");
		}
	else if(view.equals("sprintbacklog")){
		System.out.println("in het menu sprintbackl wordt geschreven:"+view);
		home.setStyleName("blauwknop");
		productbacklog.setStyleName("blauwknop");
		sprintbacklog.setStyleName("blauwknops");
		scrumboard.setStyleName("blauwknop");
		burndownchart.setStyleName("blauwknop");
				}	

	else if(view.equals("scrumboard")){
		System.out.println("in het menu wordt geschreven:"+view);
		home.setStyleName("blauwknop");
		productbacklog.setStyleName("blauwknop");
		sprintbacklog.setStyleName("blauwknop");
		scrumboard.setStyleName("blauwknops");
		burndownchart.setStyleName("blauwknop");
		}
	else if(view.equals("burndownchart")){
		System.out.println("in het menu wordt geschreven:"+view);
		home.setStyleName("blauwknop");
		productbacklog.setStyleName("blauwknop");
		sprintbacklog.setStyleName("blauwknop");
		scrumboard.setStyleName("blauwknop");
		burndownchart.setStyleName("blauwknops");
		}
	
		}
	
}



	














